#!/bin/sh /etc/rc.common

START=99
STOP=10

[ "$(uci get unblockmusic.@unblockmusic[0].strict_mode)" -eq "1" ] && strict_mode="-s"
[ "$(uci get unblockmusic.@unblockmusic[0].set_netease_server_ip)" -ne "0" ] && netease_server_ip="-f $(uci get unblockmusic.@unblockmusic[0].netease_server_ip)"
[ "$(uci get unblockmusic.@unblockmusic[0].enable_proxy)" -ne "0" ] && proxy_server_ip="-u $(uci get unblockmusic.@unblockmusic[0].proxy_server_ip)"
[ "$(uci get unblockmusic.@unblockmusic[0].pub_access)" -ne "0" ] && addr="0.0.0.0" || addr="$(uci get network.lan.ipaddr)"

enable="$(uci get unblockmusic.@unblockmusic[0].enabled)"
type="$(uci get unblockmusic.@unblockmusic[0].musicapptype)"
http_port="$(uci get unblockmusic.@unblockmusic[0].http_port)"
https_port="$(uci get unblockmusic.@unblockmusic[0].https_port)"

set_firewall(){
	if [ "${set_type}" = "start" ]; then
		echo "ipset=/.music.163.com/music" >> /etc/dnsmasq.conf
		/etc/init.d/dnsmasq restart >/dev/null 2>&1

		mkdir -p /var/etc
		lan_addr="$(uci get network.lan.ipaddr)"
		[ "$(uci get unblockmusic.@unblockmusic[0].ipset_forward_nohttps)" -ne "0" ] || forward_https="iptables -t nat -A cloud_music -i br-lan -p tcp --dport 443 -j DNAT --to ${lan_addr}:${https_port}"
		cat > "/var/etc/unblockmusic.include" <<-EOF
if ! ipset list music >/dev/null; then ipset create music hash:ip; fi
iptables -t nat -N cloud_music
iptables -t nat -A cloud_music -i br-lan -p tcp --dport 80 -j DNAT --to ${lan_addr}:${http_port}
${forward_https}
iptables -t nat -A prerouting_rule -i br-lan -p tcp -m set --match-set music dst -j cloud_music
		EOF
		/etc/init.d/firewall restart >/dev/null 2>&1
	elif [ "${set_type}" = "stop" ]; then
		echo "" > /var/etc/unblockmusic.include
		/etc/init.d/firewall restart >/dev/null 2>&1
		sed -i '/ipset=\/.music.163.com\/music/d' /etc/dnsmasq.conf
		/etc/init.d/dnsmasq restart >/dev/null 2>&1
	fi
}

set_hosts(){
	if [ "${set_type}" = "start" ]; then
		cfg_name="$(uci add dhcp domain)"
		uci set dhcp."${cfg_name}".name='music.163.com'
		uci set dhcp."${cfg_name}".ip="$(uci get network.lan.ipaddr)"
		uci commit dhcp

		cfg_name="$(uci add dhcp domain)"
		uci set dhcp."${cfg_name}".name='interface.music.163.com'
		uci set dhcp."${cfg_name}".ip="$(uci get network.lan.ipaddr)"
		uci commit dhcp

		cfg_name="$(uci add dhcp domain)"
		uci set dhcp."${cfg_name}".name='music.httpdns.c.163.com'
		uci set dhcp."${cfg_name}".ip="127.0.0.1"
		uci commit dhcp

		ip route add 223.252.199.10 dev lo
	elif [ "${set_type}" = "stop" ]; then
		music_id1="$(uci show dhcp| grep "interface.music.163.com"| grep -Eo "domain\[[0-9]+\]"| grep -Eo "[0-9]+")"
		uci delete dhcp.@domain["${music_id1}"]
		uci commit dhcp

		music_id2="$(uci show dhcp| grep "music.httpdns.c.163.com"| grep -Eo "domain\[[0-9]+\]"| grep -Eo "[0-9]+")"
		uci delete dhcp.@domain["${music_id2}"]
		uci commit dhcp

		music_id3="$(uci show dhcp| grep "music.163.com"| grep -Eo "domain\[[0-9]+\]"| grep -Eo "[0-9]+")"
		uci delete dhcp.@domain["${music_id3}"]
		uci commit dhcp

		ip route del 223.252.199.10
	fi
}

start()
{
	stop

	[ "${enable}" -eq "0" ] && exit 0

	if [ "${type}" = "default" ]; then
		node /usr/share/unblockmusic/app.js -a ${addr} -p ${http_port}:${https_port} ${netease_server_ip} ${proxy_server_ip} ${strict_mode} >/tmp/unblockmusic.log 2>&1 &
	elif [ "${type}" = "all" ]; then
		node /usr/share/unblockmusic/app.js -a ${addr} -p ${http_port}:${https_port} -o kuwo migu xiami kugou qq joox netease ${netease_server_ip} ${proxy_server_ip} ${strict_mode} >/tmp/unblockmusic.log 2>&1 &
	else
		node /usr/share/unblockmusic/app.js -a ${addr} -p ${http_port}:${https_port} -o ${type} ${netease_server_ip} ${proxy_server_ip} ${strict_mode} >/tmp/unblockmusic.log 2>&1 &
	fi

	[ "$(uci get unblockmusic.@unblockmusic[0].enable_ipset_forward)" -ne "0" ] && set_type="start" && set_firewall
	[ "$(uci get unblockmusic.@unblockmusic[0].use_hosts)" -ne "0" ] && set_type="start" && set_hosts

	/usr/share/unblockmusic/logcheck.sh >/dev/null 2>&1 &
}

stop()
{
	kill -9 "$(ps | grep app.js | grep -v grep | awk '{print $1}')" >/dev/null 2>&1
	kill -9 "$(ps | grep logcheck.sh | grep -v grep | awk '{print $1}')" >/dev/null 2>&1
	rm -f /tmp/unblockmusic.log
	set_type="stop" && set_firewall && set_hosts
}
